﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Mani.Patient.Business.Services;
using Mani.Patient.Business.Domain.Entity;

public partial class questiond : System.Web.UI.Page {


    public IQuestionService QuestionService { get; set; }

    protected override void InitializeCulture() {
        MyCulture.SetCulture();
        base.InitializeCulture();
    }
    
    protected void Page_Load(object sender, EventArgs e) {
        if (!Page.IsPostBack) {
            LoadQuestionTypes();
        }
    }

    private void LoadQuestionTypes() {
        divQuestionArea.Visible = false;

        ddlQuestionTypes.Items.Clear();
        ddlQuestionTypes.Items.Add(new ListItem("---", "-1"));
        ddlQuestionTypes.DataSource = QuestionService.GetQuestionTypes();
        ddlQuestionTypes.DataBind();
    }

    private void LoadQuestions() {

        if (ddlQuestionTypes.SelectedValue == "-1") {
            divQuestionArea.Visible = false;
            return;
        }

        divQuestionArea.Visible = true;

        gvQuestions.DataSource = QuestionService.GetQuestionsByType( Convert.ToInt32(ddlQuestionTypes.SelectedValue));
        gvQuestions.DataBind();
    }

    protected void gvQuestions_RowDeleting(object sender, GridViewDeleteEventArgs e) {

        int Id = Convert.ToInt32(gvQuestions.DataKeys[e.RowIndex].Value);
        QuestionService.DeleteQuestion(QuestionService.GetQuestion(Id));
        LoadQuestions();
    }

    protected void gvQuestions_RowDataBound(object sender, GridViewRowEventArgs e) {
        if (e.Row.RowType != DataControlRowType.DataRow)
            return;

        Control deleteButton = e.Row.FindControl("lbDeleteQuestion");

        if (deleteButton != null) {
            string message = GetLocalResourceObject("DeleteMessage").ToString();   
            ((LinkButton)deleteButton).OnClientClick = String.Format("javascript:return confirm('{0}');", message);
            // آیا نسبت به حذف سوال اطمینان دارید؟
        }
    }

    protected void ddlQuestionTypes_SelectedIndexChanged(object sender, EventArgs e) {
        LoadQuestions();
    }

    protected void btnSave_Click(object sender, EventArgs e) {

        QuestionType qt = new QuestionType();
        qt.Description = txtQuestionType.Text;
        QuestionService.CreateQuestionType(qt);
        txtQuestionType.Text = String.Empty;
        LoadQuestionTypes();
    }

    protected void btnSaveQuestion_Click(object sender, EventArgs e) {
        Question q = new Question();
        q.QuestionDesc = txtQuestion.Text;
        q.QuestionType = QuestionService.GetQuestionType(Convert.ToInt32(ddlQuestionTypes.SelectedValue));

        QuestionService.CreateQuestion(q);
        txtQuestion.Text = String.Empty;
        LoadQuestions();

    }

    protected void lbDeleteQuestionType_Click(object sender, EventArgs e) {
        if (ddlQuestionTypes.SelectedValue == "-1")
            return;

        QuestionService.DeleteQuestionType( QuestionService.GetQuestionType(Convert.ToInt32(ddlQuestionTypes.SelectedValue)) );
        LoadQuestionTypes();
    }
}
